﻿using HuiZe.education.Model;
using HuiZe.education.Resource.Controls;
using HuiZe.education.ViewModel;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace HuiZe.education.View
{
    /// <summary>
    /// ShareResourceView.xaml 的交互逻辑
    /// </summary>
    public partial class ShareResourceView : UserControl
    {
        public ShareResourceViewModel mainView { get; set; }

        public ShareResourceView()
        {
            InitializeComponent();
            _list_res = new List<resShareList>();
            Loaded += AccountLoginView_Loaded;
            pageManage.LoadDataCallback = updata;
            PART_Search.PreviewMouseLeftButtonUp += PART_Search_PreviewMouseLeftButtonUp;
        }

        void PART_Search_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (e.OriginalSource != null && e.OriginalSource is Button)
            {
                updata();
            }
        }

        private void AccountLoginView_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            ShareResourceViewModel mainViewModel = DataContext as ShareResourceViewModel;
            mainView = mainViewModel;

            updata();
            //updata(0);
            //pageManage.ItemSourceCount = mainViewModel.pageCount;
        }

        void updata()
        {
            updata(1);
        }
        void updata(int _page)
        {
            updata(_current_type, _page, PART_Search.Text);
        }
        void updata(int type, int _page = 1, string _key = "")
        {
            //ImageList.Children.Clear();
            //var list = mainView.GetShareList(type, 1);
            //foreach (var item in list)
            //{
            //    mybkBookEdit3 book = new mybkBookEdit3();
            //    book.NomalImage = item.image_cover;
            //    book.bookName = item.title;
            //    book.shareName = item.nickname;
            //    book.createTime = item.createtime_text.ToString();
            //    book.Margin = new Thickness(10, 10, 0, 0);
            //    book.Id = item.bclass_id.ToString();
            //    book.Height = 250;
            //    book.Width = 200;
            //    book.DownLoadUrl = item.zippath;
            //    book.QuoteImg.MouseLeftButtonDown += quoteImg_MouseDown;
            //    book.DownImg.MouseLeftButtonUp += DownImg_MouseDown;
            //    ImageList.Children.Add(book);
            //}

            _list_res = mainView.GetShareList(type, _page, _key);
            PART_MyShareItems.ItemsSource = _list_res;

            pageManage.Visibility = (_list_res != null && _list_res.Count > 0) ? Visibility.Visible : Visibility.Collapsed;
            if (pageManage.Visibility == Visibility.Visible)
            {
                pageManage.ItemSourceCount = mainView.pageCount;
                if (_page_count_last != mainView.pageCount)
                    pageManage.UpdataPages();

                _page_count_last = mainView.pageCount;
            }
        }

        /// <summary>
        /// 备课包引用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void quoteImg_MouseDown(object sender, MouseButtonEventArgs e)
        {
            ShareResourceViewModel attendClass = DataContext as ShareResourceViewModel;

            FrameworkElement __do = sender as FrameworkElement;
            mybkBookEdit3 bookbtn2 = new mybkBookEdit3();
            while (__do != null)
            {
                __do = __do.Parent as FrameworkElement;
                if (__do.Parent.ToString() == "HuiZe.education.Resource.Controls.mybkBookEdit3")
                {
                    bookbtn2 = __do.Parent as mybkBookEdit3;
                    break;
                }
                if (__do.Parent == null) break;
            }
            if (!string.IsNullOrWhiteSpace(bookbtn2.Id))
            {
                attendClass.BkResourcesQuote(Convert.ToInt32(bookbtn2.Id));
            }


        }

        void DownImg_MouseDown(object sender, MouseButtonEventArgs e)
        {
            ShowDialogView3 showDialogView = new ShowDialogView3();
            showDialogView.ShowDialogContent = "是否下载该资源包";
            if (showDialogView.ShowDialog() == true)
            {
                ShareResourceViewModel attendClass = DataContext as ShareResourceViewModel;
                var button = sender;
                FrameworkElement __do = sender as FrameworkElement;
                mybkBookEdit3 bookbtn = new mybkBookEdit3();

                while (__do != null)
                {
                    __do = __do.Parent as FrameworkElement;
                    if (__do.Parent.ToString() == "HuiZe.education.Resource.Controls.mybkBookEdit3")
                    {
                        bookbtn = __do.Parent as mybkBookEdit3;
                        break;
                    }
                    if (__do.Parent == null) break;
                }

                if (!string.IsNullOrWhiteSpace(bookbtn.Id))
                {
                    attendClass.DownBkResource(Convert.ToInt32(bookbtn.Id), bookbtn.DownLoadUrl);
                }
            }
        }

        void CustomRadioButton_Click(object sender, RoutedEventArgs e)
        {
            CustomRadioButton customRadio = sender as CustomRadioButton;
            string cotent = customRadio.Content.ToString();
            _current_type = 0;
            if (cotent == "全部")
            {
                _current_type = 0;
            }
            if (cotent == "公开")
            {
                _current_type = 1;
            }
            if (cotent == "本校")
            {
                _current_type = 2;
            }
            updata();
        }

        void PageManage_PreviewMouseUp_1(object sender, MouseButtonEventArgs e)
        {
            //if (e.OriginalSource.ToString().Contains("CustomRadioButton"))
            //{
            //    CustomRadioButton button = e.OriginalSource as CustomRadioButton;
            //    string index = button.Content.ToString();
            //    if (index == ">")
            //    {
            //        //index = pageManage.Content.ToString();
            //        index = pageManage.CurrentIndex < pageManage.ItemSourceCount ? (pageManage.CurrentIndex + 1).ToString() : pageManage.CurrentIndex.ToString();
            //    }
            //    if (index == "<")
            //    {
            //        index = pageManage.CurrentIndex > 1 ? (pageManage.CurrentIndex - 1).ToString() : "1";
            //    }
            //    if (index == "首页")
            //    {
            //        index = "1";
            //    }
            //    if (index == "尾页")
            //    {
            //        index = pageManage.ItemSourceCount.ToString();
            //    }
            //    //mainView.GetShareList(0, Convert.ToInt32(index));
            //    updata(Convert.ToInt32(index));
            //}
        }

        void PART_MyShare_Item_Loaded(object sender, RoutedEventArgs e)
        {
            PART_MyShare_Item _item = (PART_MyShare_Item)sender;
            _item.DownloadCallback = MyShare_Download;
            _item.ReferenceCallback = MyShare_Reference;
            _item.SelectedCallback = MyShare_Selected;
            _item.Load_Image();
        }

        void MyShare_Reference(resShareList _data)
        {
            if (_data == null) return;

            mainView.BkResourcesQuote(_data.bclass_id);
        }

        void MyShare_Download(resShareList _data)
        {
            if (_data == null) return;

            ShowDialogView3 showDialogView = new ShowDialogView3();
            showDialogView.ShowDialogContent = "是否下载该资源包";
            if (showDialogView.ShowDialog() == true)
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Filter = "Zip包|*.zip";
                sfd.FilterIndex = 1;
                sfd.FileName = Path.GetFileName(_data.zippath);
                sfd.RestoreDirectory = true;
                if (sfd.ShowDialog() == true)
                {
                    Win_MyShare_Download _win = new Win_MyShare_Download(_data.zippath, sfd.FileName);
                    _win.Owner = Application.Current.MainWindow;
                    if (_win.ShowDialog() == true)
                    {
                        mainView.ShowDialogView("下载成功!");
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(_win.ErrorMessage))
                        {
                            mainView.ShowDialogView($"下载备课包失败!\r\n{_win.ErrorMessage}");
                        }
                    }
                }
                //mainView.DownBkResource(_data.bclass_id, _data.zippath);
            }
        }

        void MyShare_Selected(resShareList _data)
        {
            Win_Lesson_MyShare_Viewer _win = new Win_Lesson_MyShare_Viewer();
            _win.Owner = Application.Current.MainWindow;
            _win.Loaded += (sender, e) =>
            {
                var _list = mainView.GetClassResourceDetail(_data.bclass_id.ToString());
                if (_list != null && _list.Count > 0)
                {
                    _win.Load(_list, _list.Select(x => x.id).First());
                }
            };
            _win.ShowDialog();
        }

        int _current_type;
        int _page_count_last;
        List<resShareList> _list_res;
    }
}
